package com.aida.evil.eda.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Adapter.CommonAdapter;
import com.aida.evil.eda.Adapter.ViewHolder;
import com.aida.evil.eda.Recommend.AppRecommend;
import com.aida.evil.eda.Window.CustomDialog;

import java.util.ArrayList;
import java.util.List;

import cn.smssdk.gui.layout.SizeHelper;

/****************************************************
 * com.aida.evil.eda.fragment.About
 * 作者：白雨濃  evil
 * 时间：16-2-12 11:23
 * 邮箱：evilbai@foxmail.com
 * 页面用途：关于页面，包含使用说明，关于软件和软件推荐
 ****************************************************/
public class About extends Fragment
{
    private String[] aboutitem_strings = {"使用说明", "关于软件", "软件推介"};

    private View view;
    private ListView listView;
    private List<ItemBean> aboutitem_list = new ArrayList<>();

    /**
     * 数据初始化
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        for (int i = 0; i < aboutitem_strings.length; i++)
        {
            aboutitem_list.add(new ItemBean(aboutitem_strings[i]));
        }
    }

    /**
     * 控件初始化
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.mainactivity_fragment_about, container, false);

        listView = (ListView) view.findViewById(R.id.mainactivity_fragment_about_listview);
        listView.setAdapter(new About_Adapter(getContext(), aboutitem_list, R.layout.mainactivity_fragment_about_listview_item));

        onClick();/*点击事件*/

        return view;
    }

    /**
     * 点击事件
     */
    private void onClick()
    {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                switch (position)
                {
                    case 0:
                    {
                        item_0();
                    }
                    break;
                    case 1:
                    {
                        item_1();
                    }
                    break;
                    case 2:
                    {
                        item_2();
                    }
                    break;
                }
            }
        });
    }

    /**
     * 第一个item的事件
     * 使用说明
     */
    private void item_0()
    {
        CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
        builder.setTitle(aboutitem_strings[0]);
        builder.setContentView(ExplainDialog.create(getContext()
                , getResources().getString(R.string._Explain)));
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * 第二个item的事件
     * 关于软件
     */
    private void item_1()
    {
        CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
        builder.setTitle(aboutitem_strings[1]);
        /*获取版本信息*/
        String versionName = null;
        int versionCode = 0;
        {
            try
            {
                // ---get the package info---
                PackageManager packageManager = getContext().getPackageManager();
                PackageInfo packageInfo = packageManager
                        .getPackageInfo(getContext().getPackageName(), 0);
                versionName = packageInfo.versionName;
                versionCode = packageInfo.versionCode;
            }
            catch (Exception e)
            {
                Log.e("VersionInfo", "Exception", e);
            }
        }
        builder.setContentView(
                AboutDialog.create(getContext()
                        , getResources().getString(R.string.app_name)
                        , versionCode, versionName));
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * 第三个item的事件
     * 软件推介
     */
    private void item_2()
    {
        startActivity(new Intent(getContext(), AppRecommend.class));
    }


    /***************************************************************
     * 动态布局
     */
    private static class ExplainDialog
    {
        /**
         * 使用说明的动态布局
         *
         * @param context
         * @param content 使用说明的文本
         * @return 布局
         */
        protected static LinearLayout create(Context context, String content)
        {
            LinearLayout linearLayout = new LinearLayout(context);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(params);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            {
                TextView textView = new TextView(context);
                textView.setText(content);
                SizeHelper.prepare(context);
                int padding_0 = SizeHelper.fromDp(0);
                int padding_20 = SizeHelper.fromDp(20);
                textView.setTextSize(15);
                textView.setPadding(padding_20, padding_0, padding_20, padding_0);
                textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
                linearLayout.addView(textView);
            }
            return linearLayout;
        }
    }

    /**
     * 关于软件的动态布局
     * item_1()
     *
     * @context
     */
    private static class AboutDialog
    {
        protected static LinearLayout create(Context context
                , String appName, int versionCode, String versionName)
        {
            SizeHelper.prepare(context);
            int padding_0 = SizeHelper.fromDp(0);
            int padding_20 = SizeHelper.fromDp(20);

            LinearLayout linearLayout = new LinearLayout(context);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(params);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            {
                LinearLayout linearLayout_0 = new LinearLayout(context);
                LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams
                        (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout_0.setGravity(Gravity.CENTER);
                linearLayout_0.setLayoutParams(params0);
                linearLayout_0.setPadding(padding_20, padding_20, padding_20, padding_20);
                linearLayout_0.setOrientation(LinearLayout.HORIZONTAL);
                {
                    TextView textView_1 = new TextView(context);
                    textView_1.setText("应用名：");
                    TextView textView_2 = new TextView(context);
                    textView_2.setText(appName);
                    linearLayout_0.addView(textView_1);
                    linearLayout_0.addView(textView_2);
                }
                linearLayout.addView(linearLayout_0);

                LinearLayout linearLayout_1 = new LinearLayout(context);
                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams
                        (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout_1.setGravity(Gravity.CENTER);
                linearLayout_1.setLayoutParams(params1);
                linearLayout_1.setPadding(padding_20, padding_20, padding_20, padding_20);
                linearLayout_1.setOrientation(LinearLayout.HORIZONTAL);
                {
                    TextView textView_1 = new TextView(context);
                    textView_1.setText("版本号：");
                    TextView textView_2 = new TextView(context);
                    textView_2.setText("" + versionCode);
                    linearLayout_1.addView(textView_1);
                    linearLayout_1.addView(textView_2);
                }
                linearLayout.addView(linearLayout_1);

                LinearLayout linearLayout_2 = new LinearLayout(context);
                LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams
                        (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout_2.setGravity(Gravity.CENTER);
                linearLayout_2.setLayoutParams(params2);
                linearLayout_2.setPadding(padding_20, padding_0, padding_20, padding_20);
                linearLayout_2.setOrientation(LinearLayout.HORIZONTAL);
                {
                    TextView textView_1 = new TextView(context);
                    textView_1.setText("版本名：");
                    TextView textView_2 = new TextView(context);
                    textView_2.setText(versionName);
                    linearLayout_2.addView(textView_1);
                    linearLayout_2.addView(textView_2);
                }
                linearLayout.addView(linearLayout_2);
            }

            return linearLayout;
        }
    }


    /**
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     * 私有内部类
     */

    private class About_Adapter extends CommonAdapter<ItemBean>
    {

        public About_Adapter(Context context, List<ItemBean> itemBean_list, int itemLayoutResId)
        {
            super(context, itemBean_list, itemLayoutResId);
        }

        @Override
        public void convert(ViewHolder viewHolder, ItemBean item)
        {
            TextView textView = viewHolder.getView(R.id.mainactivity_fragment_about_listview_textview);
            textView.setText(item.getList_aboutitemtext());
        }
    }

    private class ItemBean
    {
        private String list_aboutitemtext;

        public ItemBean(String list_aboutitemtext)
        {
            this.list_aboutitemtext = list_aboutitemtext;
        }

        public String getList_aboutitemtext()
        {
            return list_aboutitemtext;
        }
    }


}
